<template>
    <rui-page :defines="defines">
        <rui-form :form-options="searchFormOptions" :read-only="false" ref="searchForm" @keydown.enter.native="handleSearch"></rui-form>
        <Row style="text-align: center;">
            <Button @click="handleSearch" type="primary">搜索</Button>
            <Button @click="handleReset">重置</Button>
        </Row>
        <br/>
        <Row>
            <Alert show-icon>
                已选择 <span class="select-count">{{selectCount}}</span> 项
                <a class="select-clear" @click="clearSelectAll">清空</a>
            </Alert>
        </Row>
        <rui-table
            :defineId="'defineExtras'"
            :columns="['extrasProjectNo','extrasProjectName','isExistsFather','fatherNo']"
            :showIndex="false"
            :select="true"
            @loadDatas="queryData"
            @on-selection-change="changeSelect"
            ref="extrasQueryTable"
        >
        </rui-table>
        <Row style="text-align: center;">
            <Button @click="save" icon="md-bookmark" type="primary">保存</Button>
            <Button @click="backPage" icon="md-backspace" type="primary">返回</Button>
        </Row>
    </rui-page>
</template>
<script>
    import defineExtras from "./define/extras-define";
    import {
        getProjectData
    } from "@/projects/afs-core-business/api/afs-product/product/extrasProject";
    import {
        saveExtrasProject
    } from "@/projects/afs-core-business/api/afs-product/product/extrasProgramConfig";
    export default {
        name: "extras-project",
        props: {
            //方案ID
            extrasPlanId:{
                type:String,
            },
        },
        data() {
            return {
                open: "projectForm",
                selectCount: 0,
                modalTitle: "附加贷项目新增",
                searchFormOptions: [
                    {
                        grids: [
                            {defineId: "defineExtras", span: 12, fields: ["extrasProjectNo"]},
                            {defineId: "defineExtras", span: 12, fields: ["extrasProjectName"]},
                        ]
                    }
                ],
            };
        },
        computed: {
            defines() {
                return [
                    {
                        id: "defineExtras",
                        fields: defineExtras
                    }
                ];
            }
        },
        methods: {
            queryData(pageNumber,pageSize) {
                let queryData = {pageNumber:pageNumber,pageSize:pageSize};
                queryData["extrasPlanId"]=this.extrasPlanId;
                queryData["extrasProjectNo"]=this.$refs.searchForm.getFormData().extrasProjectNo,
                queryData["extrasProjectName"]=this.$refs.searchForm.getFormData().extrasProjectName,
                queryData["isExistsFather"]=this.$refs.searchForm.getFormData().isExistsFather,
                getProjectData(queryData).then(res => {
                    if (res.code === "0000") {
                        let {records, total} = res.data;
                        this.$refs.extrasQueryTable.updateTableData(records, total);
                    }
                });
            },
            handleSearch() {
                this.refresh();
            },
            handleReset() {
                this.$refs.searchForm.resetFrom();
                // 重新加载数据
                this.refresh();
            },
            clearSelectAll() {
                this.$refs.extrasQueryTable.getTable().selectAll(false);
            },
            changeSelect(e) {
                this.selectList = e;
                this.selectCount = e.length;
            },
            save() {
                if (this.selectCount <= 0) {
                    this.$Message.warning("您还未选择数据");
                    return;
                }
                this.$Modal.confirm({
                    title: "确认保存吗",
                    content: "您确认要保存所选的 " + this.selectCount + " 条数据?",
                    onOk: () => {
                        let ids = [];
                        let names = [];
                        this.selectList.forEach(function (e) {
                            ids += e.id + ",";
                            names += e.extrasProjectName + ",";
                        });
                        ids = ids.substring(0, ids.length - 1);
                        names = names.substring(0, names.length - 1);
                        saveExtrasProject({
                            ids: ids,
                            extrasPlanId: this.extrasPlanId,
                            extrasProjectName: names
                        }).then(res => {
                            if (res.code === "0000") {
                                this.$Message.success("设置成功");
                                this.clearSelectAll();
                                this.$emit('saveSuccess', "退回")
                            }
                        });
                    }
                });
            },
            refresh() {
                this.$refs['extrasQueryTable'].reloadData();
                this.clearSelectAll();
            },
            backPage() {
                this.$emit('backPageExtras', "退回")
            },
        },
        mounted() {
            this.refresh();
        },
    };
</script>
